<?php
	require_once('../process/connect_db.php');
	$db = new bd("admins");
	$query="Select nom_torneig From admins";
	$t=$db->consulta($query);	
	
	$trobat = false;
	for ($i=0; $i < sizeof($t) && !$trobat; $i++) { 
		if($t[$i]['nom_torneig'] == $_POST['torneig']){
			$trobat = true;
		}
	}
	
	if(!$trobat)	header("Location: index.php");
	
	$query = "Select nom_db From admins Where nom_torneig = \"". $_POST['torneig']. "\"";
	$db_torneig = $db->consulta($query);	

	$db = new bd($db_torneig[0]['nom_db']);

	print_r($_POST);
	echo "<br><br>";
	
	
	/*
	
	// COMPROVACIO GENERAL
	if(!isset($_POST['nom_equip']) || !isset($_POST['cat']) || !isset($_POST['torneig']) || $_POST['nom_equip'] == "" || $_POST['cat'] == "" || $_POST['torneig'] == "" )  {
		
		?>
		<script> 
			alert("Falta introduir totes les dades");
			location.href="../forms/f_inscrip.php?torneig="+"<?php echo $_POST['torneig'] ?>";
		</script>

		<?php

	}
	
	$dnis_entrada = array();
	$dnis_buits = array();
	
	// COMPROVACIO GENERAL + DNIs FALTANTS
	foreach ($_POST as $key => $value) {
		if($value != "" && isString($value) != 0){
			?>
			<script> 
				alert("Format de dades incorrecte");
				location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
			</script>
	
			<?php
		}	
		
		if($key != 'nom_equip' && $key != 'cat' && $key != 'torneig'){	// Es dni
		
			$aux = isDni($value);
			if($value != "" && isDni($value) != 0){
				?>
				<script> 
					alert("<?php echo $value ?> no es un dni correcte");
					location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
				</script>
				<?php
			} else if($value != ""){
				$dnis_entrada[] = $value;
				$query = "Select count(dni) From participants Where dni ='". $value ."'";
				$dnis = $db->consulta($query);
				if(sizeof($dnis))	$dnis_buits[] = $value;
			}
		}
	}
	
	// COMPROVACIO DNIs REPETITS
	$n = sizeof($dnis_entrada);
	$aux = array_unique($dnis_entrada);
	if(sizeof($aux) != $n){
				?>
				<script> 
					alert("Dnis repetits");
					location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
				</script>
				<?php
	}
	
	// COMPROVACIO CATEGORIA EXISTEIX I ELS MINs I MAXs
	$query = "Select nom From categories";
	$cats = $db->consulta($query);
	
	$trobat = false;
	for ($i=0; $i < sizeof($cats) && !$trobat; $i++) { 
		if($cats[$i]['nom'] == $_POST['cat']) $trobat = true;
	}
	
	
	if(!$trobat)  {
		?>
		<script> 
			alert("No Existeix la categoria seleccionada");
			location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
		</script>
		<?php
	}
	
	// COMPROVACIO NOMBRE DE PARTICIPANTS CORRECTE
	$query = "Select min_part, max_part From categories Where nom ='".$_POST['cat']."'";
	$res = $db->consulta($query);
	
	if(sizeof($dnis_entrada) < $res[0]['min_part'] || sizeof($dnis_entrada) > $res[0]['max_part']){
		?>
		<script> 
			alert("Nombre de participants incorrecte");
			location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
		</script>
		<?php
	}
	
	//COMPROVACIO EQUIPS EXISTEIX
	$query = "Select nom From equips Where nom ='". $_POST['nom_equip'] ."'";
	$eqs = $db->consulta($query);
	if(sizeof($eqs) > 0){
		?>
		<script> 
			alert("Nom de l'equip en us!");
			location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
		</script>
		<?php
	}
	

	//DADES OK!! SI DNIs CONEGUTS, INSERT. SINO, OMPLIR DADES! 

	if(sizeof($dnis_buits) == 0){
		// INSERIM DADES
		
		 $query="Insert into equips values ('".$_POST["nom_equip"]."','".$_POST["cat"]."','n')";
         $res = $db->executar($query);
         
         for ($i=0; $i < sizeof($dnis_entrada); $i++) { 
            $query="Insert into equip_participants values ('".$_POST["nom_equip"]."','".$dnis_entrada[$i]."')";
         	$res = $db->executar($query); 
         }
						?>
				<script> 
					alert("EQUIP INSERIT CORRECTAMENT!");
					location.href="../forms/f_inscrip.php?torneig=<?php echo $_POST['torneig'] ?>";
				</script>
				<?php

	} else {
		// PREGUNTEM PER DADES PARTICIPANTS
		
		$dir = "location: ../forms/f_nou_dni.php?torneig=". $_POST['torneig'] ."&equip=" . $_POST['nom_equip']."&cat=" . $_POST['cat'];
		for ($i=0; $i < sizeof($dnis_buits); $i++) { 
			$dir .= "&dni" . $i . "=" . $dnis_buits[$i];
		}
		header($dir);	
	
	echo "PREGUNTEM!";
	}
	
echo "<br><br>DNIS: ";
print_r($dnis_entrada);
echo "<br><br>DNIS BUITS: ";
print_r($dnis_buits);
	
	function isString($s){

		$s1 = str_replace(' ', '', $s);

		for ($i=0; $i < strlen($s1); $i++) { 
			$c = substr($s1,$i,1);
			if(!(($c >= 'a' && $c <= 'z') || ($c >= 'A' && $c <= 'Z')|| ($c >= '0' && $c <= '9')))		return -1;
		}
		
		return 0;
	}
	
	function comprobar_nif($nif){
   $letras = explode(',','T,R,W,A,G,M,Y,F,P,D,X,B,N,J,Z,S,Q,V,H,L,C,K,E');
   if (
      (strlen($nif)!=9) ||
      (!is_long($entero=intval(substr($nif,0,8)))) ||
      (!in_array($letra=strtoupper(substr($nif,8,1)),$letras)) ||
      ($letra!=$letras[$entero%23])
      ){
         return false;
      }else{
         return true;
      }
	}
	
	function isDni($d){		// -1 format dni incorrecte,  -2 les xifres i la lletra no concorden
		if(!is_string($d) || strlen($d) != 9)	return -1;
		
		$c = substr($d,8,1);
		if(!(($c >= 'a' && $c <= 'z') || ($c >= 'A' && $c <= 'Z')))		return -1;
		
		for ($i=0; $i < 8; $i++) { 
			if(!((substr($d,$i,1) >= '0' && substr($d,$i,1) <= '9')))	return -1; 
		}
		
		if(!comprobar_nif($d))	return -2;
		
		return 0;
	}
	





	*/
?>